<template>
  <div class="head" v-if="!fullscreen">
    <div class="left">
      <el-container>
        <el-header style="background: #2b2f3a">
          <!-- 顶部导航栏 -->
          <el-menu
            :default-active="this.$route.path"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            background-color="#2b2f3a"
            text-color="#fff"
            active-text-color="#ffd04b"
            ref="menus"
            menu-trigger="click"
            :unique-opened="true"
            router
          >
            <template v-for="item in routerList">
              <el-menu-item
                v-if="!item.children"
                :index="item.path"
                :key="item.name"
                >{{ item.title }}</el-menu-item
              >
              <el-submenu v-else :index="item.title" :key="item.name">
                <template slot="title">{{ item.title }}</template>
                <el-menu-item
                  v-for="(slot, idx) in item.children"
                  :key="idx"
                  :index="slot.path"
                  >{{ slot.title }}</el-menu-item
                >
              </el-submenu>
            </template>
          </el-menu>
        </el-header>
      </el-container>
    </div>
    <div class="right">
      <!-- <div>
        <el-avatar  src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"></el-avatar>张三
      </div> -->
      <el-dropdown
        class="avatar-container right-menu-item hover-effect"
        trigger="click"
      >
        <div class="avatar-wrapper">
          <img
            src="../assets/avatar.png"
            class="user-avatar"
          />
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item  @click.native="logout">
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <div class="role">{{userName}}</div>
      <el-tooltip class="item" effect="dark" content="全屏查看" placement="bottom-start">
        <img src="../assets/fullscreen.png" alt="" @click="handleFullScreen"/>
      </el-tooltip>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navBarIndex: "1-1",
      fullscreen: false,
      routerList: [
        {
          title: "数据概念",
          children: [
            {
              title: "首页",
              path: "/home",
            },
            {
              title: "广陈社区",
              path: "/community",
            },
            {
              title: "幸福指数",
              path: "/hindex",
            },
            {
              title: "投诉建议排名",
              path: "/adviceRank",
            },
          ],
        },
        {
          title: "地区管理",
          children: [
            {
              title: "网格管理",
              path: "/area",
            },
            {
              title: "社区管理",
              path: "/country",
            },
            //  {
            //   title: "小区管理",
            //   path: "/house",
            // }
          ],
        },
        {
          title: "村民信息",
          children: [
            {
              title: "干部管理",
              path: "/personnel",
            },
            {
              title: "村民管理",
              path: "/villager",
            },
          ],
        },
        {
          title: "巡查管理",
          children: [
            {
              title: "巡查人员管理",
              path: "/checker",
            },
            {
              title: "巡查数据管理",
              path: "/checkData",
            },
            {
              title: "巡查统计报表",
              path: "/checkList",
            },
            // {
            //   title: "巡查二维码",
            //   path: "/checkQrCode",
            // },
          ],
        },

        {
          title: "投诉建议",
          children: [
            {
              title: "投诉建议类型",
              path: "/adviceTypeList",
            },
            {
              title: "投诉建议报表",
              path: "/adviceList",
            },
          ],
        },
        {
          title: "信访管理",
          children: [
            {
              title: "信访信息管理",
              path: "/visitInfoList",
            },
            {
              title: "信访事件处理",
              path: "/visitEventList",
            },
          ],
        },
        {
          title: "资讯管理",
          children: [
            {
              title: "资讯列表",
              path: "/messageList",
            },
          ],
        },
        {
          title: "系统管理",
          children: [
            {
              title: "登录权限",
              path: "/authList",
            },
          ],
        },
      ],
    };
  },
  computed:{
    userName(){
      if(localStorage.userInfo){
         let obj = JSON.parse(localStorage.userInfo);
         return obj.userName;
      }
      else{
        return '';
      }
    }
  },
  mounted() {
    let that = this;
    window.onresize = function () {
      if (!that.checkFull()) {
        // 退出全屏后要执行的动作
        console.log("退出全屏");
        that.fullscreen = false;
      }
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      this.$refs.menus.close(keyPath)
    },
    checkFull() {
      //判断浏览器是否处于全屏状态 （需要考虑兼容问题）
      //火狐浏览器
      var isFull =
        document.mozFullScreen ||
        document.fullScreen ||
        //谷歌浏览器及Webkit内核浏览器
        document.webkitIsFullScreen ||
        document.webkitRequestFullScreen ||
        document.mozRequestFullScreen ||
        document.msFullscreenEnabled;
      if (isFull === undefined) {
        isFull = false;
      }
      return isFull;
    },
    // 全屏事件
    handleFullScreen() {
      this.$emit("showFullScreen", true);
      let element = document.documentElement;
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen();
        }
      }
      this.fullscreen = !this.fullscreen;
    },
    logout(){
      localStorage.removeItem("tsToken");
      localStorage.removeItem("userInfo");
      this.$router.push('/login');
    }
  },
};
</script>

<style lang="less" scoped>
.head {
  width: 100%;
  background: #2b2f3a;
  display: flex;
  align-items: center;
  justify-content: space-around;
  color: #fff;
  height:60px;
}
/deep/ .el-submenu__title {
  padding: 0 20px;
}
/deep/ .el-menu.el-menu--horizontal {
  border: none;
  display: flex;
}
/deep/ .el-menu--horizontal>.el-submenu{
  float:none;
}
.right {
  display: flex;
  align-items: center;
  .role{
    margin:0 20px 0 0;
    &+img{
      cursor: pointer;
    }
  }
  .avatar-container {
    .avatar-wrapper {
      position: relative;
      margin:5px 10px 0;
      .user-avatar {
        cursor: pointer;
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }
      .el-icon-caret-bottom {
        cursor: pointer;
        position: absolute;
        right: -20px;
        top: 25px;
        font-size: 12px;
      }
    }
  }
}
</style>